﻿using System;
using System.Collections.Generic;
using RuntimeGizmos;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

namespace Sizebox.CharacterEditor
{
	// Token: 0x02000238 RID: 568
	public abstract class BaseAdvancedView : BaseView
	{
		// Token: 0x06000C18 RID: 3096 RVA: 0x0006372C File Offset: 0x0006192C
		protected override void Awake()
		{
			base.Awake();
			this.gizmo = Camera.main.GetComponent<TransformGizmo>();
			this.hiddenKeys = new List<string>();
			this.showAllButton.onClick.AddListener(new UnityAction(this.OnShowAll));
			this.handleSizeSlider.onValueChanged.AddListener(new UnityAction<float>(this.OnHandleSize));
			this.resetAllButton.onClick.AddListener(delegate()
			{
				this.handleManager.ResetAll(this.handleView.DataId);
			});
			this.resetBoneButton.onClick.AddListener(delegate()
			{
				this.handleManager.ResetTarget(this.handleView.DataId, this.optionsGui.Options);
			});
			this.resetButton.onClick.AddListener(delegate()
			{
				this.handleManager.Reset(this.handleView.DataId, this.optionsGui.Options);
			});
			this.hideHeadToggle.onValueChanged.AddListener(new UnityAction<bool>(this.HideHeadBones));
			this.hideTorsoToggle.onValueChanged.AddListener(new UnityAction<bool>(this.HideTorsoBones));
			this.hideArmsToggle.onValueChanged.AddListener(new UnityAction<bool>(this.HideArmBones));
			this.hideLegsToggle.onValueChanged.AddListener(new UnityAction<bool>(this.HideLegBones));
		}

		// Token: 0x06000C19 RID: 3097 RVA: 0x00063858 File Offset: 0x00061A58
		protected override void OnEnable()
		{
			base.OnEnable();
			this.showingAll = false;
			this.handleManager.SetOptions(this.optionsGui.Options);
			this.handleManager.SetHandleSize(this.handleSizeSlider.value);
			this.ShowHandles(null);
		}

		// Token: 0x06000C1A RID: 3098 RVA: 0x000638A5 File Offset: 0x00061AA5
		protected override void OnDisable()
		{
			base.OnDisable();
			this.handleManager.DisableHandlesMode();
		}

		// Token: 0x06000C1B RID: 3099 RVA: 0x000638B8 File Offset: 0x00061AB8
		private void Update()
		{
			this.UpdateResetButtonText();
		}

		// Token: 0x06000C1C RID: 3100 RVA: 0x000638C0 File Offset: 0x00061AC0
		private void UpdateResetButtonText()
		{
			Text componentInChildren = this.resetButton.GetComponentInChildren<Text>();
			TransformType type = this.gizmo.type;
			if (type != TransformType.Move)
			{
				if (type != TransformType.Rotate)
				{
					if (type == TransformType.Scale)
					{
						componentInChildren.text = "Scale";
					}
				}
				else
				{
					componentInChildren.text = "Rotation";
				}
			}
			else
			{
				componentInChildren.text = "Position";
			}
		}

		// Token: 0x06000C1D RID: 3101 RVA: 0x00063930 File Offset: 0x00061B30
		protected void ShowHandles(List<string> keys = null)
		{
			if (keys == null)
			{
				if (!this.showingAll)
				{
					keys = this.handleView.Keys;
				}
				else
				{
					keys = new List<string>(this.handleManager.TargetEditor.SkeletonEdit.BoneMap.Keys);
				}
			}
			this.handleManager.DisableHandlesMode();
			this.handleManager.EnableHandlesMode(this.handleView.DataId, keys);
			if (!this.showingAll)
			{
				this.handleManager.SetHandleStyle(SkeletonEditHandle.HandleStyle.Active, keys, false);
			}
			else
			{
				this.handleManager.SetHandleStyle(SkeletonEditHandle.HandleStyle.Active, this.handleView.Keys, true);
			}
			this.handleManager.DisableHandles(this.hiddenKeys);
		}

		// Token: 0x06000C1E RID: 3102 RVA: 0x000639EA File Offset: 0x00061BEA
		private void OnShowAll()
		{
			this.showingAll = !this.showingAll;
			this.ShowHandles(null);
		}

		// Token: 0x06000C1F RID: 3103 RVA: 0x00063A02 File Offset: 0x00061C02
		private void OnHandleSize(float size)
		{
			this.handleManager.SetHandleSize(size);
		}

		// Token: 0x06000C20 RID: 3104 RVA: 0x00063A10 File Offset: 0x00061C10
		private void HideKeys(ICollection<string> keys)
		{
			foreach (string item in keys)
			{
				if (!this.hiddenKeys.Contains(item))
				{
					this.hiddenKeys.Add(item);
				}
			}
		}

		// Token: 0x06000C21 RID: 3105 RVA: 0x00063A7C File Offset: 0x00061C7C
		private void UnhideKeys(ICollection<string> keys)
		{
			foreach (string item in keys)
			{
				if (this.hiddenKeys.Contains(item))
				{
					this.hiddenKeys.Remove(item);
				}
			}
		}

		// Token: 0x06000C22 RID: 3106 RVA: 0x00063AE8 File Offset: 0x00061CE8
		private void HideHeadBones(bool hideBones)
		{
			Humanoid component = this.handleManager.TargetEditor.GetComponent<Humanoid>();
			if (!component)
			{
				return;
			}
			Transform boneTransform = component.GetComponent<Animator>().GetBoneTransform(HumanBodyBones.Head);
			HashSet<string> keys = this.FindChildBoneKeys(boneTransform, null);
			if (hideBones)
			{
				this.HideKeys(keys);
			}
			else
			{
				this.UnhideKeys(keys);
			}
			this.ShowHandles(null);
		}

		// Token: 0x06000C23 RID: 3107 RVA: 0x00063B4C File Offset: 0x00061D4C
		private void HideArmBones(bool hideBones)
		{
			Humanoid component = this.handleManager.TargetEditor.GetComponent<Humanoid>();
			if (!component)
			{
				return;
			}
			Transform boneTransform = component.GetComponent<Animator>().GetBoneTransform(HumanBodyBones.LeftUpperArm);
			Transform boneTransform2 = component.GetComponent<Animator>().GetBoneTransform(HumanBodyBones.RightUpperArm);
			HashSet<string> keys = this.FindChildBoneKeys(boneTransform, null);
			HashSet<string> keys2 = this.FindChildBoneKeys(boneTransform2, null);
			if (hideBones)
			{
				this.HideKeys(keys);
				this.HideKeys(keys2);
			}
			else
			{
				this.UnhideKeys(keys);
				this.UnhideKeys(keys2);
			}
			this.ShowHandles(null);
		}

		// Token: 0x06000C24 RID: 3108 RVA: 0x00063BD8 File Offset: 0x00061DD8
		private void HideLegBones(bool hideBones)
		{
			Humanoid component = this.handleManager.TargetEditor.GetComponent<Humanoid>();
			if (!component)
			{
				return;
			}
			Transform boneTransform = component.GetComponent<Animator>().GetBoneTransform(HumanBodyBones.LeftUpperLeg);
			Transform boneTransform2 = component.GetComponent<Animator>().GetBoneTransform(HumanBodyBones.RightUpperLeg);
			HashSet<string> keys = this.FindChildBoneKeys(boneTransform, null);
			HashSet<string> keys2 = this.FindChildBoneKeys(boneTransform2, null);
			if (hideBones)
			{
				this.HideKeys(keys);
				this.HideKeys(keys2);
			}
			else
			{
				this.UnhideKeys(keys);
				this.UnhideKeys(keys2);
			}
			this.ShowHandles(null);
		}

		// Token: 0x06000C25 RID: 3109 RVA: 0x00063C60 File Offset: 0x00061E60
		private void HideTorsoBones(bool hideBones)
		{
			Humanoid component = this.handleManager.TargetEditor.GetComponent<Humanoid>();
			if (!component)
			{
				return;
			}
			Animator component2 = component.GetComponent<Animator>();
			List<Transform> list = new List<Transform>();
			Transform boneTransform = component2.GetBoneTransform(HumanBodyBones.Head);
			if (boneTransform)
			{
				list.Add(boneTransform);
			}
			boneTransform = component2.GetBoneTransform(HumanBodyBones.LeftUpperArm);
			if (boneTransform)
			{
				list.Add(boneTransform);
			}
			boneTransform = component2.GetBoneTransform(HumanBodyBones.RightUpperArm);
			if (boneTransform)
			{
				list.Add(boneTransform);
			}
			boneTransform = component2.GetBoneTransform(HumanBodyBones.LeftLowerLeg);
			if (boneTransform)
			{
				list.Add(boneTransform);
			}
			boneTransform = component2.GetBoneTransform(HumanBodyBones.RightLowerLeg);
			if (boneTransform)
			{
				list.Add(boneTransform);
			}
			HashSet<string> keys = this.FindChildBoneKeys(component.transform, list);
			if (hideBones)
			{
				this.HideKeys(keys);
			}
			else
			{
				this.UnhideKeys(keys);
			}
			this.ShowHandles(null);
		}

		// Token: 0x06000C26 RID: 3110 RVA: 0x00063D50 File Offset: 0x00061F50
		private HashSet<string> FindChildBoneKeys(Transform inTransform, List<Transform> stopPoints = null)
		{
			if (!inTransform)
			{
				return null;
			}
			if (stopPoints == null)
			{
				stopPoints = new List<Transform>();
			}
			HashSet<string> hashSet = new HashSet<string>();
			EditBone[] componentsInChildren = inTransform.GetComponentsInChildren<EditBone>();
			List<EditBone> list = new List<EditBone>();
			foreach (EditBone editBone in componentsInChildren)
			{
				if (stopPoints.Contains(editBone.RealTransform))
				{
					list.Add(editBone);
				}
				Dictionary<EditBone, string> keyMap = this.handleManager.TargetEditor.SkeletonEdit.KeyMap;
				if (keyMap.ContainsKey(editBone))
				{
					hashSet.Add(editBone.Key);
				}
			}
			EditBone componentInParent = inTransform.GetComponentInParent<EditBone>();
			if (componentInParent && componentInParent.RealTransform == inTransform)
			{
				hashSet.Add(componentInParent.Key);
			}
			foreach (EditBone editBone2 in list)
			{
				List<EditBone> list2 = new List<EditBone>();
				editBone2.GetComponentsInChildren<EditBone>(list2);
				foreach (EditBone editBone3 in list2)
				{
					hashSet.Remove(editBone3.Key);
				}
			}
			return hashSet;
		}

		// Token: 0x04001158 RID: 4440
		[SerializeField]
		[Space(15f)]
		protected HandleManager handleManager;

		// Token: 0x04001159 RID: 4441
		[SerializeField]
		protected BaseSkeletonHandleView handleView;

		// Token: 0x0400115A RID: 4442
		[SerializeField]
		protected SkeletonEditOptionsGui optionsGui;

		// Token: 0x0400115B RID: 4443
		[Header("Visibility Options")]
		[SerializeField]
		protected Button showAllButton;

		// Token: 0x0400115C RID: 4444
		[SerializeField]
		protected Slider handleSizeSlider;

		// Token: 0x0400115D RID: 4445
		[Header("Reset Buttons")]
		[SerializeField]
		protected Button resetBoneButton;

		// Token: 0x0400115E RID: 4446
		[SerializeField]
		protected Button resetButton;

		// Token: 0x0400115F RID: 4447
		[SerializeField]
		protected Button resetAllButton;

		// Token: 0x04001160 RID: 4448
		[Header("Hide Toggles")]
		[SerializeField]
		protected Toggle hideHeadToggle;

		// Token: 0x04001161 RID: 4449
		[SerializeField]
		protected Toggle hideTorsoToggle;

		// Token: 0x04001162 RID: 4450
		[SerializeField]
		protected Toggle hideArmsToggle;

		// Token: 0x04001163 RID: 4451
		[SerializeField]
		protected Toggle hideLegsToggle;

		// Token: 0x04001164 RID: 4452
		protected bool showingAll;

		// Token: 0x04001165 RID: 4453
		protected List<string> hiddenKeys;

		// Token: 0x04001166 RID: 4454
		private TransformGizmo gizmo;
	}
}
